package cn.smxy.college.service.impl;

import cn.smxy.client.PlantClient;
import cn.smxy.college.mapper.CollegeMapper;
import cn.smxy.college.mapper.CollegePlantMapper;
import cn.smxy.college.service.CollegePlantService;
import cn.smxy.param.CollegePlantPageParam;
import cn.smxy.param.PageParam;
import cn.smxy.param.PlantPageParam;
import cn.smxy.pojo.College;
import cn.smxy.pojo.CollegePlant;
import cn.smxy.pojo.Plant;
import cn.smxy.pojo.User;
import cn.smxy.util.R;
import cn.smxy.vo.CollegePlantVo;
import cn.smxy.vo.PlantVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CollegePlantServiceImpl implements CollegePlantService {

    @Autowired
    private CollegePlantMapper collegePlantMapper;
    @Autowired
    private PlantClient plantClient;
    @Autowired
    private CollegeMapper collegeMapper;

    /**
     * 删除用户对应的校园植物作品
     *
     * @param userId
     * @return
     */
    @Override
    public R removeByUserId(Long userId) {
        QueryWrapper<CollegePlant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        int rows = collegePlantMapper.delete(queryWrapper);
        log.info("CollegePlantServiceImpl.removeByUserId业务结束，结果为:{}",rows);
        return R.ok("删除成功");
    }

    /**
     * 删除校园植物
     *
     * @param plantId
     * @return
     */
    @Override
    public R removePlant(Long plantId) {
        QueryWrapper<CollegePlant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plant_id",plantId);
        int rows = collegePlantMapper.delete(queryWrapper);
        if(rows == 0) {
            log.info("CollegePlantServiceImpl.remove业务结束，结果为:{}","删除失败");
            return R.fail("删除失败");
        }
        log.info("CollegePlantServiceImpl.remove业务结束，结果为:{}","删除成功");
        return R.ok("删除成功");
    }

    /**
     * 查看植物校园信息
     *
     * @param plantId
     * @return
     */
    @Override
    public R info(Long plantId) {
        QueryWrapper<CollegePlant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plant_id",plantId);
        CollegePlant collegePlant = collegePlantMapper.selectOne(queryWrapper);
        if(collegePlant == null) {
            return R.ok("",null);
        }else {
            CollegePlantVo collegePlantVo = new CollegePlantVo(collegePlant);
            College college = collegeMapper.selectById(collegePlant.getCollegeId());
            collegePlantVo.setCollegeName(college.getCollegeName());
            log.info("CollegePlantServiceImpl.info业务结束，结果为:{}", collegePlantVo);
            return R.ok(collegePlantVo);
        }
    }

    /**
     * 分页展示校园植物
     *
     * @param collegePlantPageParam
     * @return
     */
    @Override
    public R page(CollegePlantPageParam collegePlantPageParam) {
        // 参数拆分
        Long collegeId = collegePlantPageParam.getCollegeId();
        int currentPage = collegePlantPageParam.getCurrentPage();
        int pageSize = collegePlantPageParam.getPageSize();
        QueryWrapper<CollegePlant> queryWrapper = new QueryWrapper<>();
        if (collegeId != null) {
            queryWrapper.eq("college_id", collegeId);
        }
        IPage<CollegePlant> page = new Page<>(currentPage, pageSize);
        page = collegePlantMapper.selectPage(page, queryWrapper);
        long total = page.getTotal();
        List<CollegePlant> records = page.getRecords();
        List<CollegePlantVo> collegePlantVoList = new ArrayList<>();
        for (CollegePlant collegePlant : records) {
            CollegePlantVo collegePlantVo = new CollegePlantVo(collegePlant);
            // 查询植物和作者相关信息
            PlantVo plantVo = plantClient.info(collegePlant.getPlantId());
            collegePlantVo.setPlantName(plantVo.getPlantName());
            collegePlantVo.setPlantPicture(plantVo.getPlantPicture());
            collegePlantVo.setAddress(plantVo.getAddress());
            collegePlantVo.setCreateTime(plantVo.getCreateTime());
            collegePlantVo.setUserName(plantVo.getUserName());
            // 查询高校名称
            College college = collegeMapper.selectById(collegePlant.getCollegeId());
            collegePlantVo.setCollegeName(college.getCollegeName());

            collegePlantVoList.add(collegePlantVo);
        }
        log.info("CollegePlantServiceImpl.page业务结束，结果为:{}", collegePlantVoList);
        return R.ok("查询成功", collegePlantVoList, total);
    }

    /**
     * 保存校园植物
     *
     * @param collegePlant
     * @return
     */
    @Override
    public R save(CollegePlant collegePlant) {
        QueryWrapper<CollegePlant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plant_id", collegePlant.getPlantId());
        CollegePlant selectOne = collegePlantMapper.selectOne(queryWrapper);
        if (selectOne != null) {
            collegePlant.setId(selectOne.getId());
            collegePlantMapper.updateById(collegePlant);
        } else {
            int rows = collegePlantMapper.insert(collegePlant);
            if (rows == 0) {
                log.info("CollegePlantServiceImpl.save业务结束，结果为:{}", "校园植物保存失败");
                return R.fail("校园植物保存失败");
            }
        }
        log.info("CollegePlantServiceImpl.save业务结束，结果为:{}", "校园植物保存成功");
        return R.ok("保存成功");
    }
}
